import pyautogui,csv,time

#Set these to the correct coordinates for your particular computer.3
schoolField=(439,486)
submitButton=(474,544)
submitButtonColor=(49,123,253)
submitAnotherLink=(478,422)

#customer list from csv file
customers=[]

#convert to dictionary variable
partyFile=open('partyEnglish.csv',encoding='utf-8')
#利用reader()方法，将csv文件转化为便于读写的格式
partyReader=csv.reader(partyFile)

#read data from csv file
for row in partyReader:

    #跳过第一行
    if partyReader.line_num==1:
        continue
    
    #例：某个行[3]为'3 5'
    #提取字符串'3 5'
    food=row[3]
    #将字符串按空格分割,并返回一个列表 ['3','5']
    foods=food.split()
    
    #开始构造列表customers中的每一个元素customer
    customer={
        'school':row[0],
        'attend':row[1],
        'headcount':row[2],
        'foodOption':foods,
        'taboos':row[4],
        'contact':row[5]
    }

    #放入列表customers
    customers.append(customer)

#关闭csv文件
partyFile.close()

#slow down pace of pyautogui action
pyautogui.PAUSE=1

#get form window on focus
pyautogui.click((100,100))

#full screen
pyautogui.hotkey('F11')

#automate filling in form
for customer in customers:

    #To confirm the form is ready to fill in  检查是否到表格页面

    #To bottom of the page 直接翻到页面低端
    pyautogui.hotkey('Fn','End')

    #Wait until the form page has loaded.
    #如果底部颜色不是submit的颜色，就继续等，等到网页响应成功
    while not pyautogui.pixelMatchesColor(submitButton[0],submitButton[1],submitButtonColor):
        time.sleep(0.5)

    #To top of the page for input
    pyautogui.hotkey('Fn','Home')
    schoolList=['少林','武当','昆仑','峨眉','五岳','逍遥','丐帮','全真','古墓','移花宫']
    school=int(customer['school'])
    print('Entering {} info……'.format(schoolList[school-1]))

    #Click the first field
    pyautogui.click(schoolField[0],schoolField[1])

    #Fill out the 门派 field
    for order in range(school):
        pyautogui.press('down')#向下键，选择下一个选项
    pyautogui.press('enter')
    pyautogui.press('\t')#tab键，下一题

    #Fill out the 是否参加 field
    if customer['attebnd']=='1':
        pyautogui.typewrite(['space','\t'])#空格键，填充选择题

        #Fill out 多少人 field
        pyautogui.typewrite(customer['headcount']+'\t',0.25)#控制操作间隔为0.25

        #Fill out 食物 field
        # 1-肉夹馍;2-山东大饼;3-手撕饼;4-煎饼果子;5-馒头;6-肉包子;7-素馅包子
        if '1' in customer['foodOption']:
            pyautogui.typewrite(['space','\t'])
        else:
            pyautogui.press('\t')

        if '2' in customer['foodOption']:
            pyautogui.typewrite(['space','\t'])
        else:
            pyautogui.press('\t')

        if '3' in customer['foodOption']:
            pyautogui.typewrite(['space','\t'])
        else:
            pyautogui.press('\t')
    
        if '4' in customer['foodOption']:
            pyautogui.typewrite(['space','\t'])
        else:
            pyautogui.press('\t')

        if '5' in customer['foodOption']:
            pyautogui.typewrite(['space','\t'])
        else:
            pyautogui.press('\t')

        if '6' in customer['foodOption']:
            pyautogui.typewrite(['space','\t'])
        else:
            pyautogui.press('\t')
        
        #Fill out 禁忌 field.
        pyautogui.typewrite(customer['taboos']+'\t',0.25)

        #Fill out 手机号码 field.
        pyautogui.typewrite(customer['contact']+'\t',0.25)

        #Wait until form page has loaded.
        print('Clicked Submit')




    elif customer['attend']=='2':
        pyautogui.typewrite(['down','space','\t'])
        pyautogui.click(100,100)

        #To bottom of the page
        pyautogui.hotkey('Fn','End')
        pyautogui.click(submitButton)

    elif customer['attend']=='3':
        pyautogui.typewrite(['down','dowm','space','\t'])
        pyautogui.click(100,100)

        #To bottom of the page
        pyautogui.hotkey('Fn','End')
        pyautogui.click(submitButton)
    

    #Click the Submit another response link.
    pyautogui.click(submitAnotherLink[0],submitAnotherLink[1])



   